package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.RequisitionDetail;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2016/5/12.
 */
public class TripMultipleListAdapter extends ArrayAdapter<RequisitionDetail> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2016-05-12 13:54:41 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  Map<Integer, Boolean> isCheckMap = new HashMap<Integer, Boolean>();
  private String tripIds;

  private static class ViewHolder {
    public final LinearLayout rootView;
    public final LinearLayout llHolderOneSimpleList;
    public final TextView tvTitleOneSimpleList;
    public final TextView tvContentOneSimpleList;
    public final LinearLayout llHolderTwoSimpleList;
    public final TextView tvTitleTwoSimpleList;
    public final TextView tvContentTwoSimpleList;
    public final LinearLayout llHolderThreeSimpleList;
    public final TextView tvTitleThreeSimpleList;
    public final TextView tvContentThreeSimpleList;
    public final LinearLayout llHolderFourSimpleList;
    public final TextView tvTitleFourSimpleList;
    public final TextView tvContentFourSimpleList;
    public final CheckBox cbTrip;

    private ViewHolder(LinearLayout rootView, LinearLayout llHolderOneSimpleList,
        TextView tvTitleOneSimpleList, TextView tvContentOneSimpleList,
        LinearLayout llHolderTwoSimpleList, TextView tvTitleTwoSimpleList,
        TextView tvContentTwoSimpleList, LinearLayout llHolderThreeSimpleList,
        TextView tvTitleThreeSimpleList, TextView tvContentThreeSimpleList,
        LinearLayout llHolderFourSimpleList, TextView tvTitleFourSimpleList,
        TextView tvContentFourSimpleList, CheckBox cbTrip) {
      this.rootView = rootView;
      this.llHolderOneSimpleList = llHolderOneSimpleList;
      this.tvTitleOneSimpleList = tvTitleOneSimpleList;
      this.tvContentOneSimpleList = tvContentOneSimpleList;
      this.llHolderTwoSimpleList = llHolderTwoSimpleList;
      this.tvTitleTwoSimpleList = tvTitleTwoSimpleList;
      this.tvContentTwoSimpleList = tvContentTwoSimpleList;
      this.llHolderThreeSimpleList = llHolderThreeSimpleList;
      this.tvTitleThreeSimpleList = tvTitleThreeSimpleList;
      this.tvContentThreeSimpleList = tvContentThreeSimpleList;
      this.llHolderFourSimpleList = llHolderFourSimpleList;
      this.tvTitleFourSimpleList = tvTitleFourSimpleList;
      this.tvContentFourSimpleList = tvContentFourSimpleList;
      this.cbTrip = cbTrip;
    }

    public static ViewHolder create(LinearLayout rootView) {
      LinearLayout llHolderOneSimpleList =
          (LinearLayout) rootView.findViewById(R.id.ll_holderOne_simpleList);
      TextView tvTitleOneSimpleList = (TextView) rootView.findViewById(R.id.tv_titleOne_simpleList);
      TextView tvContentOneSimpleList =
          (TextView) rootView.findViewById(R.id.tv_contentOne_simpleList);
      LinearLayout llHolderTwoSimpleList =
          (LinearLayout) rootView.findViewById(R.id.ll_holderTwo_simpleList);
      TextView tvTitleTwoSimpleList = (TextView) rootView.findViewById(R.id.tv_titleTwo_simpleList);
      TextView tvContentTwoSimpleList =
          (TextView) rootView.findViewById(R.id.tv_contentTwo_simpleList);
      LinearLayout llHolderThreeSimpleList =
          (LinearLayout) rootView.findViewById(R.id.ll_holderThree_simpleList);
      TextView tvTitleThreeSimpleList =
          (TextView) rootView.findViewById(R.id.tv_titleThree_simpleList);
      TextView tvContentThreeSimpleList =
          (TextView) rootView.findViewById(R.id.tv_contentThree_simpleList);
      LinearLayout llHolderFourSimpleList =
          (LinearLayout) rootView.findViewById(R.id.ll_holderFour_simpleList);
      TextView tvTitleFourSimpleList =
          (TextView) rootView.findViewById(R.id.tv_titleFour_simpleList);
      TextView tvContentFourSimpleList =
          (TextView) rootView.findViewById(R.id.tv_contentFour_simpleList);
      CheckBox cbTrip = (CheckBox) rootView.findViewById(R.id.cb_trip);
      return new ViewHolder(rootView, llHolderOneSimpleList, tvTitleOneSimpleList,
          tvContentOneSimpleList, llHolderTwoSimpleList, tvTitleTwoSimpleList,
          tvContentTwoSimpleList, llHolderThreeSimpleList, tvTitleThreeSimpleList,
          tvContentThreeSimpleList, llHolderFourSimpleList, tvTitleFourSimpleList,
          tvContentFourSimpleList, cbTrip);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_trip_multiple, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    RequisitionDetail item = getItem(position);
    //Bind your data to the views here
    vh.tvTitleOneSimpleList.setText(R.string.tripReason);
    vh.tvTitleTwoSimpleList.setText(R.string.planBeginTime);
    vh.tvTitleThreeSimpleList.setText(R.string.planEndTime);
    vh.tvTitleFourSimpleList.setText(R.string.naturalTripDays);

    vh.tvContentOneSimpleList.setText(item.getBtripReason());
    vh.tvContentTwoSimpleList.setText(item.getPlanSTime());
    vh.tvContentThreeSimpleList.setText(item.getPlanETime());
    vh.tvContentFourSimpleList.setText(item.getNaturalDays());
    if (tripIds != null) {
      vh.cbTrip.setVisibility(View.GONE);
    }
    vh.cbTrip.setTag(position);
    // 找到需要选中的条目
    if (isCheckMap != null && isCheckMap.containsKey(position)) {
      vh.cbTrip.setChecked(isCheckMap.get(position));
    } else {
      vh.cbTrip.setChecked(false);
    }

    vh.cbTrip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      private int mCheckPosition;

      @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mCheckPosition = Integer.parseInt(buttonView.getTag().toString());
        if (isChecked) {
          // 将选中的放入hashmap中
          isCheckMap.put(mCheckPosition, isChecked);
        } else {
          // 取消选中的则剔除
          isCheckMap.remove(mCheckPosition);
        }
      }
    });

    return vh.rootView;
  }

  public boolean isCheckedItem(int position) {
    if (isCheckMap != null && isCheckMap.containsKey(position)) {
      return true;
    } else {
      return false;
    }
  }

  private LayoutInflater mInflater;

  // Constructors
  public TripMultipleListAdapter(Context context, List<RequisitionDetail> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  public TripMultipleListAdapter(Context context, List<RequisitionDetail> objects, String tripIds) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.tripIds = tripIds;
  }

  public TripMultipleListAdapter(Context context, RequisitionDetail[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }
}


